// pages/comment/comment.js
Page({
    
    /**
     * 页面的初始数据
     */
    data: {
        src:'../../static/not_save.png',
        tar_url:"/pages/comment-c/comment-c",
        add_url:"/pages/addcomment/addcomment",
        dianzan: [{ dzzs: '22', collected: 0, id: 1 }],
        diancai: [{ dccs: '24', collected: 0, id: 1 }],


        data1:[
            {
                comment_id:0,
                comment_pr_id: null,
                 comment_user_avatar:"../../static/p2.png",
                comment_user_name:"Mary",
                comment_time:"2022-11-03",
                comment_text:"非常好非常好",
                reply_id: 0, 
                parent_id: 0, 
                reply_name: '',
                comment_dianzan: 8,
                collected1: 0,
                comment_daincai: 5, 
                collected2: 0
            },
            {
                comment_id:1,
                comment_pr_id: null,
                 comment_user_avatar:"../../static/p2.png",
                comment_user_name:"Jone",
                comment_time:"2021-11-04",
                comment_text:"还可以",
                reply_id: 0, 
                parent_id: 0, 
                reply_name: '',
                comment_dianzan:7,
                collected1: 0,
                comment_daincai: 2, 
                collected2: 0,
            },
            {
                comment_id:2,
                comment_pr_id: null,
                 comment_user_avatar:"../../static/p2.png",
                comment_user_name:"Grace",
                comment_time:"2021-11-05",
                comment_text:null,
                reply_id: 0, 
                parent_id: 0, 
                reply_name: '',
                comment_dianzan:12,
                collected1: 0,
                comment_daincai: 7, 
                collected2: 0,
            }
        ]
    },
    dateSort: function (e) {
        var property = e.currentTarget.dataset.property;
        var self = this;
        var data1 = self.data.data1;
        var sortRule = true; // 正序倒序
        self.setData({
          data1: data1.sort(self.compare(property, sortRule))
        })
        console.log(data1)
      },
      //热度排序
      dateSorthot: function (e) {
        var property = e.currentTarget.dataset.property;
        var self = this;
        var data1 = self.data.data1;
        var sortRule = false; // 正序倒序
        self.setData({
          data1: data1.sort(self.compare(property, sortRule))
        })
        console.log(data1)
      },
    compare: function (property, bol) {
        return function (a, b) {
            if (property == 'comment_time') { //  判断为点赞排序还是时间排序
               var value1 = Date.parse(new Date(a[property]));
               var value2 = Date.parse(new Date(b[property]));
			} else {
                var value1 = a[property];
                var value2 = b[property];
            }
        if(bol){
          return value1 - value2;
        }else {
          return value2 - value1;
        }
      }
     
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        that.setData({
          ['data1[2].comment_text']:options.textData
          
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    Tap(event){
        var tar_url=this.data.tar_url
        console.log(tar_url)
        wx.navigateTo({
          url: tar_url,
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    onCollectionTap1: function (event) {
        // 获取当前点击下标
        var index = event.currentTarget.dataset.index;
        // data中获取列表
        var dianzan = this.data.dianzan;
        for (let i in dianzan) { //遍历列表数据
        if (i == index) { //根据下标找到目标
        var collectStatus = false
        if (dianzan[i].collected == 0) { //如果是没点赞+1
        collectStatus = true
        dianzan[i].collected = parseInt(dianzan[i].collected) + 1
        dianzan[i].dzzs = parseInt(dianzan[i].dzzs) + 1
        } else {
        collectStatus = false
        dianzan[i].collected = parseInt(dianzan[i].collected) - 1
        dianzan[i].dzzs = parseInt(dianzan[i].dzzs) - 1
        }
        wx.showToast({
        title: collectStatus ? '点赞成功' : '取消点赞',
        })
        }
        }
        this.setData({
            dianzan: dianzan
        })
        },


        onCollectionTap2: function (event) {
            // 获取当前点击下标
            var index = event.currentTarget.dataset.index;
            // data中获取列表
            var diancai = this.data.diancai;
            for (let i in diancai) { //遍历列表数据
            if (i == index) { //根据下标找到目标
            var collectStatus = false
            if (diancai[i].collected == 0) { //如果是没点赞+1
            collectStatus = true
            diancai[i].collected = parseInt(diancai[i].collected) + 1
            diancai[i].dccs = parseInt(diancai[i].dccs) + 1
            } else {
            collectStatus = false
            diancai[i].collected = parseInt(diancai[i].collected) - 1
            diancai[i].dccs = parseInt(diancai[i].dccs) - 1
            }
            wx.showToast({
            title: collectStatus ? '点踩成功' : '取消点踩',
            })
            }
            }
            this.setData({
                diancai:diancai
            })
            },
    /**
     * 用户点击右上角分享
     */
    plonCollectionTap1: function (event) {
        // 获取当前点击下标
        var index = event.currentTarget.dataset.index;
        // data中获取列表
        var data1 = this.data.data1;
        for (let i in data1) { //遍历列表数据
        if (i == index) { //根据下标找到目标
        var collectStatus = false
        if (data1[i].collected1 == 0) { //如果是没点赞+1
        collectStatus = true
        data1[i].collected1 = parseInt(data1[i].collected1) + 1
        data1[i].dzzs = parseInt(data1[i].dzzs) + 1
        } else {
        collectStatus = false
        data1[i].collected1 = parseInt(data1[i].collected1) - 1
        data1[i].dzzs = parseInt(data1[i].dzzs) - 1
        }
        wx.showToast({
        title: collectStatus ? '点赞成功' : '取消点赞',
        })
        }
        }
        this.setData({
            data1: data1
        })
        },


        plonCollectionTap2: function (event) {
            // 获取当前点击下标
            var index = event.currentTarget.dataset.index;
            // data中获取列表
            var data1 = this.data.data1;
            for (let i in data1) { //遍历列表数据
            if (i == index) { //根据下标找到目标
            var collectStatus = false
            if (data1[i].collected2 == 0) { //如果是没点赞+1
            collectStatus = true
            data1[i].collected2 = parseInt(data1[i].collected2) + 1
            data1[i].dccs = parseInt(data1[i].dccs) + 1
            } else {
            collectStatus = false
            data1[i].collected2 = parseInt(data1[i].collected2) - 1
            data1[i].dccs = parseInt(data1[i].dccs) - 1
            }
            wx.showToast({
            title: collectStatus ? '点踩成功' : '取消点踩',
            })
            }
            }
            this.setData({
                data1:data1
            })
            },

    onShareAppMessage() {

    },
    change_save:function(){
        if(this.data.src=='../../static/not_save.png'){
            this.setData({
                src:'../../static/save.png'
            })
            wx.showToast({
                title:'已收藏'
            })
        }
            else{
                this.setData({
                    src:'../../static/not_save.png'
                })
                wx.showToast({
                    title:'已取消'
                })
            }
        },
        goto_add(event){

            var add_url=this.data.add_url
            console.log(add_url)
            wx.navigateTo({
              url: add_url,
            })
        },

})